<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../lib/axios.js"></script>
</head>
<body>
    <h1>注册</h1>
    用户名: <input type="text" class="user"><br>
    密  码: <input type="text" class="pwd"><br>
    手机号:<select name="" id="">
        <option value="">+86</option>
        <option value="">+87</option>
        <option value="">+88</option>
        <option value="">+89</option>
    </select>
     <input type="text" class="phone"><br>
    <input type="text" class="code"><button class="codeBtn">获取验证码</button><br>
    <button class="reg">注册</button>
    <a href="./login.html">去登录</a>
    <script>
        let user=document.querySelector('.user');
        let pwd=document.querySelector('.pwd');
        let phone=document.querySelector('.phone');
        let code=document.querySelector('.code');
        let codeBtn=document.querySelector('.codeBtn');
        let reg=document.querySelector('.reg')
        // 定义一个存放验证码的变量
        let codes;
        // 手机号正则
        let get=/^1[3-9]\d{9}$/
        // 给获取验证码添加点击事件
        codeBtn.onclick = function(e) {
            // 阻止默认行为
            e.preventDefault()
            // 判断手机号正则
            if(get.test(phone.value.trim())){
                axios.get('/regis').then((res) => {
                    console.log(res.data);
                    codes=res.data;
                    let n=6;
                   let time= setInterval(()=>{
                        codeBtn.innerHTML=--n+'后获取';
                        codeBtn.disabled=true;
                        if(n<=0){
                            clearInterval(time)
                            codeBtn.innerHTML='请重新获取'
                            codeBtn.disabled=false
                        }
                    },1000)
                })
            }   
        }
        // 给注册添加点击事件
        reg.onclick = function(e) {
            console.log('123');
            e.preventDefault()
            axios.post("/regin",{user:user.value,pwd:pwd.value}).then(res=>{
                console.log(res.data);
                
            })
        }
    </script>
</body>
</html>